package com.hankol.material.api;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.OrientationEventListener;

public class ScreenRotateListener extends OrientationEventListener {
    private static final String TAG = "ScreenRotateListener";
    private Context mContext;

    public ScreenRotateListener(Context context) {
        super(context);
        this.mContext = context;
    }

    public ScreenRotateListener(Context context, int rate) {
        super(context, rate);
        this.mContext = context;
    }

    @Override
    public void onOrientationChanged(int orientation) {
        int screenOrientation = mContext.getResources().getConfiguration().orientation;

        if (((orientation >= 0) && (orientation < 45)) || (orientation > 315)) {    //设置竖屏
            if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                Log.d(TAG, "onOrientationChanged: 设置竖屏");
            }
        } else if (orientation > 225 && orientation < 315) { //设置横屏
            if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                Log.d(TAG, "onOrientationChanged: 设置横屏");
            }
        } else if (orientation > 45 && orientation < 135) {// 设置反向横屏
            if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                Log.d(TAG, "onOrientationChanged: 设置反向横屏");
            }
        } else if (orientation > 135 && orientation < 225) { //反向竖屏
            if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                Log.d(TAG, "onOrientationChanged: 反向竖屏");
            }
        }
    }
}